package com.artPreter.service.impl;

import com.artPreter.pojo.Artist;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.artPreter.mapper.ContentArticleRelativeMapper;
import com.artPreter.pojo.Article;
import com.artPreter.mapper.ArticleMapper;
import com.artPreter.pojo.ContentArticleRelative;
import com.artPreter.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 文章内容表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-21
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ContentArticleRelativeMapper contentArticleRelativeMapper;

    /**
     * 添加文章
     * @param article
     */
    @Override
    public void add(Article article) {
        String uuid = UUID.randomUUID().toString();
        article.setArticleId(uuid);
        this.baseMapper.insert(article);
    }

    /**
     * 编辑文章
     * @param article
     */
    @Override
    public void edit(Article article) {
        this.baseMapper.updateById(article);
    }

    /**
     * 软删除文章
     * @param article
     */
    @Override
    public void delete(Article article) {
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(Article::getId,article.getId());
        List<Article> articles = this.baseMapper.selectList(articleLambdaQueryWrapper);
        if(articles.size()>0){
            Article deleteItem = articles.get(0);
            if(deleteItem.getArticleId()!=null&&!"".equals(deleteItem.getArticleId())){
                // 删除文章关联
                LambdaQueryWrapper<ContentArticleRelative> contentArticleRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
                contentArticleRelativeLambdaQueryWrapper.eq(ContentArticleRelative::getArticleId,deleteItem.getArticleId());
                contentArticleRelativeMapper.delete(contentArticleRelativeLambdaQueryWrapper);
            }
            this.baseMapper.deleteById(deleteItem);
        }
    }

    /**
     * 文章分页查询
     * @param currentPage
     * @param size
     * @param article
     * @return
     */
    @Override
    public IPage<Article> getPage(Integer currentPage, Integer size, Article article) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus,1);
        queryWrapper.orderByDesc(Article::getId);
        // 内容id
        if(article.getPlateId()!=null&&!"".equals(article.getPlateId())){
            queryWrapper.eq(Article::getPlateId,article.getPlateId());
        }
        if(article.getPlateType()!=null&&!"".equals(article.getPlateType())){
            queryWrapper.eq(Article::getPlateType,article.getPlateType());
        }


        IPage<Article> articleIPage = new Page<>(currentPage,size);
        articleIPage = this.baseMapper.selectPage(articleIPage,queryWrapper);
        return articleIPage;
    }

    @Override
    public void addToRelative(Article article) {
        ContentArticleRelative contentArticleRelative = new ContentArticleRelative();
        if(article.getArticleId()!=null&&!"".equals(article.getArticleId())){
            contentArticleRelative.setArticleId(article.getArticleId());
        }else {
            return;
        }
        if(article.getContentId()!=null&&!"".equals(article.getContentId())){
            contentArticleRelative.setContentId(article.getContentId());
        }else {
            return;
        }
        LambdaQueryWrapper<ContentArticleRelative> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContentArticleRelative::getContentId,article.getContentId());
        queryWrapper.eq(ContentArticleRelative::getArticleId,article.getArticleId());
        if(contentArticleRelativeMapper.selectCount(queryWrapper)>0){
            return;
        }else{
            contentArticleRelativeMapper.insert(contentArticleRelative);
        }
    }



    @Override
    public void removeArticleRelative(Article article) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(article.getArticleId()!=null&&!"".equals(article.getArticleId())){
            queryWrapper.eq("article_id",article.getArticleId());
        }else {
            return;
        }
        if(article.getContentId()!=null&&!"".equals(article.getContentId())){
            queryWrapper.eq("content_id",article.getContentId());
        }else {
            return;
        }
        contentArticleRelativeMapper.delete(queryWrapper);
    }

    @Override
    public IPage<Article> getPageNoRelative(Integer currentPage, Integer size, Article article) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus,1);
        queryWrapper.orderByDesc(Article::getId);
        // 内容id
        String contentId = article.getContentId();
        queryWrapper.notExists("SELECT 1 FROM jv_content_article_relative WHERE jv_content_article_relative.content_id = '"+contentId+"' and jv_content_article_relative.article_id = jv_article.article_id");
        queryWrapper.eq(Article::getPlateId,article.getPlateId());
        IPage<Article> articleIPage = new Page<>(currentPage,size);
        // 模糊搜索文章标题
        if (article.getName()!= null &&!"".equals(article.getName())) {
            queryWrapper.like(Article::getName,article.getName());
        }

        articleIPage = this.baseMapper.selectPage(articleIPage, queryWrapper);
        return articleIPage;
    }

    @Override
    public IPage<Article> getPageRelative(Integer currentPage, Integer size, Article article) {
        IPage<Article> articleIPage = new Page<>(currentPage,size);
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("s.content_id",article.getContentId());
        // 艺术家名字模糊搜索
        if(article.getName()!=null&&!article.getName().equals("")&&!article.getName().equals("null")){
            articleQueryWrapper.like("b.name",article.getName());
            System.out.println("模糊搜索名字");
        }
        // 排序
        articleQueryWrapper.orderByDesc("s.sort");
        articleIPage = this.baseMapper.getRelatedArticlePageList(articleIPage,articleQueryWrapper);
        return articleIPage;
    }

    @Override
    public IPage<Article> getPageRelativeFull(Integer currentPage, Integer size, Article article) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus,1);
        queryWrapper.orderByDesc(Article::getId);
        // 内容id
        String contentId = article.getContentId();
        queryWrapper.exists("SELECT 1 FROM jv_content_article_relative WHERE jv_content_article_relative.content_id = '"+contentId+"' and jv_content_article_relative.article_id = jv_article.article_id");
        IPage<Article> articleIPage = new Page<>(currentPage,size);
        // 模糊搜索文章标题
        if (article.getName()!= null &&!"".equals(article.getName())) {
            queryWrapper.like(Article::getName,article.getName());
        }


        articleIPage = this.baseMapper.selectPage(articleIPage, queryWrapper);
        return articleIPage;
    }

    @Override
    public IPage<Article> getPageNoRelativeFull(Integer currentPage, Integer size, Article article) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus,1);
        queryWrapper.orderByDesc(Article::getId);
        // 内容id
        String contentId = article.getContentId();
        queryWrapper.notExists("SELECT 1 FROM jv_content_article_relative WHERE jv_content_article_relative.content_id = '"+contentId+"' and jv_content_article_relative.article_id = jv_article.article_id");
        IPage<Article> articleIPage = new Page<>(currentPage,size);
        // 模糊搜索文章标题
        if (article.getName()!= null &&!"".equals(article.getName())) {
            queryWrapper.like(Article::getName,article.getName());
        }

        articleIPage = this.baseMapper.selectPage(articleIPage, queryWrapper);
        return articleIPage;
    }

    @Override
    public void deleteByArtistId(String artistId) {
        if(artistId!=null&&!"".equals(artistId)){
            LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Article::getPlateId,artistId);
            List<Article> articles = this.baseMapper.selectList(queryWrapper);
            for (int i = 0; i < articles.size(); i++) {
                this.delete(articles.get(i));
            }
        }
    }

    @Override
    public void saveSortInArticleRelative(List<Article> articleList) {
        for (int i = 0; i < articleList.size(); i++) {
            Article article = articleList.get(i);
            if(article.getContentId()!=null&&article.getArticleId()!=null&&article.getCtSort()!=null){
              ContentArticleRelative contentArticleRelative = new ContentArticleRelative();
              LambdaQueryWrapper<ContentArticleRelative> contentArticleRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
              contentArticleRelativeLambdaQueryWrapper.eq(ContentArticleRelative::getContentId,article.getContentId());
              contentArticleRelativeLambdaQueryWrapper.eq(ContentArticleRelative::getArticleId,article.getArticleId());
              contentArticleRelative.setSort(article.getCtSort());
                contentArticleRelativeMapper.update(contentArticleRelative,contentArticleRelativeLambdaQueryWrapper);

            }
        }
    }
}
